package com.sann.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * username：通过@Value("${jdbc.username}")这种SpringEL表达式注入
 * pwd：通过创建DataSource这个Bean时入参注入，@Value("${jdbc.password}")String pwd
 * DriverClass：通过Spring提供的一个让我们自定义Bean注入Spring容器自身组件的方式来注入
 *
 */
@PropertySource("classpath:mysql.properties")
public class JdbcConfig implements EmbeddedValueResolverAware {

    public JdbcConfig(){
        System.out.println("JdbcConfig 构造器调用......");
    }

    @Value("${jdbc.username}")
    private String username;

    private String driver ;

    /**
     * Spring容器会自动给入参注入 StringValueResolver。
     * 这种方式采取的也是Spring自动注入方式，跟Spring给我们标注组件注解（比如我们在类上标注@Component）的Bean里面即使是有参构造器，
     * Spring也会从容器给我们注入入参类型的Bean是一个道理。
     *
     */
    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        String driverClass = resolver.resolveStringValue("${jdbc.driver}");
        this.driver = driverClass;
    }

    /**
     * @Profile("test") 表示 “test” 环境此处配置的Bean可注入容器。context.getEnvironment().setActiveProfiles("test");
     */
    @Profile("test")
    @Bean("testDataSource")
    public DataSource dataSourceTest(@Value("${jdbc.password}") String pwd) throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(username);
        dataSource.setPassword(pwd);
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/spring");
        return dataSource;
    }

    /**
     * @Profile("dev") 表示 “dev” 环境此处配置的Bean可注入容器。context.getEnvironment().setActiveProfiles("dev");
     */
    @Profile("dev")
    @Bean("devDataSource")
    public DataSource dataSourceDev(@Value("${jdbc.password}") String pwd) throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(username);
        dataSource.setPassword(pwd);
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/spring");
        return dataSource;
    }

    /**
     * @Profile("prod") 表示 “prod” 环境此处配置的Bean可注入容器。context.getEnvironment().setActiveProfiles("prod");
     */
    @Profile("prod")
    @Bean("prodDataSource")
    public DataSource dataSourceProd(@Value("${jdbc.password}") String pwd) throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(username);
        dataSource.setPassword(pwd);
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/spring");
        return dataSource;
    }


}
